package component.fy.com.common_module.utils;

import android.content.Context;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.Toast;

/**
 * Created by Administrator on 2017/10/20 0020.
 * 需要在Application中初始化
 */

public class ToastUtil {
    private static final String TAG = "ToastUtil";
    private static Toast toast;
    private static Context context;

    private ToastUtil() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 初始化
     * @param context
     */
    public static void init(Context context) {
        ToastUtil.context = context;
    }

    /**
     * 获取ApplicationContext
     *
     * @return ApplicationContext
     */
    public static Context getContext() {
        if (context != null) return context;
        throw new NullPointerException("u should init first");
    }

    public static void showToast(String msg) {
        if (toast == null) {
//            LogUtil.e(TAG, "toast is null");
            toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        } else {
//            LogUtil.e(TAG, "toast not null");
            toast.setText(msg);
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        toast.show();
    }

    /**
     * 长时间显示的Toast
     *
     * @param msg
     */
    public static void showToastLong(String msg) {
        if (toast == null) {
//            LogUtil.e(TAG, "toast is null");
            toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        } else {
//            LogUtil.e(TAG, "toast not null");
            toast.setText(msg);
            toast.setDuration(Toast.LENGTH_LONG);
        }
        toast.show();
    }

    /**
     * 显示SnakeBar提示框
     *
     * @param view
     * @param msg
     */
    public static void showSnakeBar(View view, String msg) {
        Snackbar.make(view, msg, Snackbar.LENGTH_SHORT).show();
    }

    /**
     * 显示SnakeBar提示框
     *
     * @param view
     * @param msg
     */
    public static void showSnakeBar(View view, String msg, String actStr, View.OnClickListener onClickListener) {
        Snackbar.make(view, msg, Snackbar.LENGTH_SHORT)
                .setAction(actStr, onClickListener).show();
    }
}
